import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class KlantWijzigenGUI extends JFrame implements ActionListener
{	private Bank bank;
	private Klant klant;
	private JPanel p1,p2;
	private JComboBox klanten;
	private JPasswordField pf;
	private JTextField tf1, tf2;
	private JLabel l0, l1,l2,l3,l4,l5;
	private JButton b1, b2;
	private int hoogte=200, breedte=350;
	
	public KlantWijzigenGUI(Bank b)
	{	bank = b;
		Container c = this.getContentPane();
		c.setLayout(new FlowLayout());
		
		klanten = new JComboBox(b.alleKlanten());
		c.add(klanten);
		klanten.addActionListener(this);
		b2 = new JButton("Cancel");
		c.add(b2);
		
		b2.addActionListener(this);
		
		//visibility and such
		this.setSize(new Dimension(breedte, hoogte));
		setTitle("Klant wijzigen");
		this.setVisible(true);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	public KlantWijzigenGUI(Bank b, String n)
	{	//the whole container
		bank = b;
		klant = (Klant) bank.klanten().get(n);
		Container c = this.getContentPane();
		c.setLayout(new BorderLayout());
		
		//panels
		p1 = new JPanel();
		p1.setLayout(new GridLayout(4,2));
		c.add(p1,BorderLayout.CENTER);
		p2 = new JPanel();
		p2.setLayout(new FlowLayout());
		c.add(p2,BorderLayout.SOUTH);
		
		//add Components
		b1 = new JButton("Wijzig klantgegevens");
		p2.add(b1);
		b2 = new JButton("Cancel");
		p2.add(b2);
		l1 = new JLabel("Naam:");
		p1.add(l1);
		l2 = new JLabel(n);
		p1.add(l2);
		l3 = new JLabel("Wachtwoord:");
		p1.add(l3);
		pf = new JPasswordField("",5);
		p1.add(pf);
		pf.setText(klant.getWachtwoord());
		l4 = new JLabel("Adres:");
		p1.add(l4);
		tf1 = new JTextField("",5);
		p1.add(tf1);
		tf1.setText(klant.getAdres());
		l5 = new JLabel("Woonplaats:");
		p1.add(l5);
		tf2 = new JTextField("",5);
		p1.add(tf2);
		tf2.setText(klant.getPlaats());
		l0 = new JLabel(" ");
		l0.setForeground(Color.RED);
		c.add(l0,BorderLayout.NORTH);
		
		//actionListeners
		b1.addActionListener(this);
		b2.addActionListener(this);
		
		//visibility and such
		this.setSize(new Dimension(breedte, hoogte));
		setTitle("Klant wijzigen");
		this.setVisible(true);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e)
	{	if(e.getSource() == klanten)
		{	String naam = (String) bank.alleKlanten().get(klanten.getSelectedIndex());
			new KlantWijzigenGUI(bank, naam);
			dispose();
		}
		else if(e.getSource() == b2)
			dispose();
		else if(e.getSource() == b1)
		{	String n, a, p, w;
			n = klant.getNaam();
			a = tf1.getText();
			p = tf2.getText();
			w = new String(pf.getPassword());
			if(a.equals(""))
				l0.setText("Vul een adres in.");
			else if(p.equals(""))
				l0.setText("Vul een plaatsnaam in.");
			else if(w.equals(""))
				l0.setText("Vul een wachtwoord in.");
			else
			{	((Klant)(bank.klanten().get(n))).setAdres(a);
				((Klant)(bank.klanten().get(n))).setPlaats(p);
				((Klant)(bank.klanten().get(n))).setWachtwoord(w);
				tf1.setText("");
				tf2.setText("");
				pf.setText("");
				l0.setText("Klantgegevens zijn gewijzigd.");
			}
		}
	}
}